package com.xju.subjectsystem.controller;

import com.xju.subjectsystem.pojo.Books;
import com.xju.subjectsystem.pojo.Course;
import com.xju.subjectsystem.pojo.PageResult;
import com.xju.subjectsystem.pojo.Result;
import com.xju.subjectsystem.service.CourseService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * -*- coding:utf-8 -*-
 *
 * @projectName: Subjectsystem
 * @package: com.xju.subjectsystem.controller
 * @className: CourseController
 * @author: 15171
 * @description: TODO : 
 * @date: 2024-12-08 23:01
 */
@RestController
@RequestMapping("/course")
public class CourseController {
    private final CourseService courseService;

    public CourseController(CourseService courseService) {
        this.courseService = courseService;
    }


    /**
     * @methodName: selectByName
     * @package: com.xju.subjectsystem.controller
     * @description: TODO : 根据课程名分页查询
     * @author: 15171
     * @date: 2024-12-18 17:14
     * @param: page
     * @param: pageSize
     * @param: cname
     * @return: Result
     */
    @GetMapping("/selectByName")
    public Result selectByName(Integer page, Integer pageSize, String cname) {
        PageResult<Course> selectResult = courseService.selectByName(page, pageSize, cname);
        if (selectResult.getTotal() == 0) {
            return Result.error("该课程不存在");
        } else {
            return Result.success(selectResult);
        }
    }
    /**
     * @methodName: selectByDepart
     * @package: com.xju.subjectsystem.controller
     * @description: TODO : 根据学院名分页模糊查询
     * @author: 15171
     * @date: 2024-12-18 17:15
     * @param: page
     * @param: pageSize
     * @param: depart
     * @return: Result
     */

    @GetMapping("/selectByDepart")
    public Result selectByDepart(Integer page, Integer pageSize, String depart) {
        PageResult<Course> selectResult = courseService.selectByDepart(page, pageSize, depart);
        return Result.success(selectResult);
    }
    /**
     * @methodName: showCourse
     * @package: com.xju.subjectsystem.controller
     * @description: TODO : 展示所有课程
     * @author: 15171
     * @date: 2024-12-18 17:15

     * @return: Result
     */

    @GetMapping("/showAllCourse")
    public Result showCourse() {
        List<Course> courseList = courseService.showAllCourse();
        return Result.success(courseList);
    }
    /**
     * @methodName: batchAddCourses
     * @package: com.xju.subjectsystem.controller
     * @description: TODO :
     * @author: 72929
     * @date: 2024-12-18 18:50
     * @param: courseList
     * @return: Result
     */
    @PostMapping("/add")
    public Result batchAddCourses(@RequestBody List<Course> courseList) {

        Map<String, List<String>> resultData = courseService.batchAddCourses(courseList);

        List<String> successList = resultData.get("successList");
        List<String> failList = resultData.get("failList");


        if (successList.isEmpty()) {
            return Result.error("全部导入失败", resultData);
        } else if (!failList.isEmpty()) {
            return Result.success("部分导入成功", resultData);
        } else {
            return Result.success("全部导入成功", resultData);
        }

    }

    /**
     * @description: TODO : 更新课程信息
     * @author: 72929
     * @date: 2024-12-07 02:04
     * @param: [course]
     * @return: com.xju.subjectsystem.pojo.Course
     */
    @PutMapping("/update")
    public Result update(@RequestBody Course course) {
        Result result;

        System.out.println(course);
        result = Result.success();
        return result;
    }

    /**
     * @methodName: delete
     * @package: com.xju.subjectsystem.controller
     * @description: TODO : 删除课程
     * @author: 72929
     * @date: 2024-12-07 12:27
     * @param: course
     * @return: Result
     */
    @DeleteMapping("/deleteByCno")
    public Result deleteByCno(@RequestBody Course course) {
        Result result;

        try {
            courseService.delete(course);
            result = Result.success();
        } catch (Exception e) {
            return Result.error("删除失败，格式有误");
        }

        return result;
    }
}
